package sg.edu.ntu.metaweb;

import sg.edu.ntu.metaweb.http.HTTPServer;
import sg.edu.ntu.metaweb.pastry.PastryNodeHandle;

public class MetawebNode {

	private static final int CONFIG_HTTP_PORT = 3441;
	private static final int CONFIG_BIND_PORT = 9001;
	private static final String CONFIG_BOOT_ADDRESS = "155.69.151.200";
	private static final int CONFIG_BOOT_PORT = 9001;
	
	private HTTPServer httpServer;
	private PastryNodeHandle pastryNodeHandle;
	
	public MetawebNode(int httpPort, int bindPort, String bootAddress, int bootPort) {
		this.pastryNodeHandle = new PastryNodeHandle(bindPort, bootAddress, bootPort);
		
		if (!pastryNodeHandle.initilize()) {
			System.out.println("Unable to initialize PastryNodeHandle.");
			System.exit(0);
		}
			
		httpServer = new HTTPServer(httpPort, this.pastryNodeHandle);
	}

	
	private static void printUsage() {
		System.err.println("Usage: ...");
	}
	
	public static void main(String args[]) {
		
		MetawebNode metawebNode;
		
		if (args.length < 4) {
			metawebNode = new MetawebNode(MetawebNode.CONFIG_HTTP_PORT, 
										  MetawebNode.CONFIG_BIND_PORT, 
										  MetawebNode.CONFIG_BOOT_ADDRESS, 
										  MetawebNode.CONFIG_BOOT_PORT);
		}
		else {
			try {
				int httpPort = Integer.parseInt(args[0]);
				int bindPort = Integer.parseInt(args[1]);
				String bootAddress = args[2];
				int bootPort = Integer.parseInt(args[3]);
				
				metawebNode = new MetawebNode(httpPort, bindPort, bootAddress, bootPort);
			} catch(NumberFormatException nfe) {
			    MetawebNode.printUsage();
			    System.exit(0);
			}
		}
		
	}
	
}
